using UnityEngine;
using System.Collections;

public class ParticleEffectContainer : MonoBehaviour
{
	private GameObject[] m_aoEffectInstances;
	public int m_iNumberOfInstances;
	public int m_iLastSpawnedInstance;
	
	public void initialize()
	{
		// create all instances
		m_aoEffectInstances = new GameObject[m_iNumberOfInstances];
		GameObject oTemplate = (GameObject)Instantiate(gameObject);
		Destroy(oTemplate.GetComponent<ParticleEffectContainer>());
		
		// stop the particle systems
		for(int i=0; i<m_iNumberOfInstances; ++i)
		{
			m_aoEffectInstances[i] = (GameObject)Instantiate(oTemplate);
			
			object[] aoParticleSystems = m_aoEffectInstances[i].GetComponents<ParticleSystem>();
			foreach(object o in aoParticleSystems)
			{
				ParticleSystem  ps = (ParticleSystem)o;
				ps.Stop();
			}		
		}
	}
	
	public void spawn(Vector3 _vPosition, Quaternion _vRotation)
	{
		m_iLastSpawnedInstance = (m_iLastSpawnedInstance + 1) % m_iNumberOfInstances;
		object[] aoParticleSystems = m_aoEffectInstances[m_iLastSpawnedInstance].GetComponents<ParticleSystem>();
		foreach(object o in aoParticleSystems)
		{
			ParticleSystem  ps = (ParticleSystem)o;
			ps.transform.position = _vPosition;
			ps.transform.rotation = _vRotation;
			ps.Clear();
			ps.Play();
		}
	}	
	
	// Update is called once per frame
	void update ()
	{
	
	}
}
